package com.desire.controller.sys;

import com.desire.bean.common.ResultBean;
import com.desire.bean.sys.Dept;
import com.desire.common.validation.group.Modify;
import com.desire.common.validation.group.Save;
import com.desire.controller.common.BaseController;
import com.desire.service.sys.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-14 周六 16:43
 * @since 17
 */
@RestController
@Api(tags = "部门管理")
@RequestMapping("/api/dept")
public class DeptController extends BaseController {
    @Autowired
    DeptService deptService;

    @PostMapping("/save")
    @ApiOperation(value = "新增部门", notes = "添加部门，验证信息")
    public ResultBean save(@RequestBody @Validated(Save.class) Dept dept) {
        int rs = deptService.save(dept);
        return rs > 0 ? success("保存成功") : success("保存失败");
    }

    @PostMapping("/queryChild")
    @ApiOperation(value = "查询子部门信息", notes = "查询子部门 返回子部门的所有信息")
    public ResultBean queryChild(@RequestBody Map<String, Object> map) {
        List<Map<String, Object>> maps = deptService.queryChildDept(map);
        return success("查询子部门信息成功", maps);
    }

    @PostMapping("/queryTree")
    @ApiOperation(value = "查询部门树形结构", notes = "树形结构")
    public ResultBean queryTree(@RequestBody Map<String, Object> map) {
        List<Map<String, Object>> maps = deptService.queryTree(map);
        return success("查询子部门信息成功", maps);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改部门", notes = "更改部门信息")
    public ResultBean update(@Validated(Modify.class) @RequestBody Dept dept) {
        int rs = deptService.update(dept);
        return rs > 0 ? success("修改成功") : success("修改失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除部门", notes = "删除部门信息，逻辑删除，修改isdel字段为1")
    public ResultBean delete(String id) {
        int rs = deptService.delete(id);
        if (rs == 1) {
            return success("删除成功");
        } else if (rs == 2) {
            return fail("删除失败，该部门下有子部门");
        } else {
            return fail("删除失败");
        }
    }

}
